#!/bin/bash

function promptfor
{
	if [ @@ = @$1@ ]
	then
		echo Parameter \"--"$4"\" was not specified on command line
		echo -n "$3 [$2]: "
		read v
		if [ @@ = @$v@ ]
		then
			rval=$2
		else
			rval=$v
		fi
	else
		rval=$1
	fi
}
startt=`date +%s`
extra=""
devstr="unknown"
srate=""
source=""
ra=""
dec=""
freq=""
rfgain=""
runtime=""
datadir=""
acnt=0
while [ $# -gt 1 ]
do
	acnt=`expr $acnt + 1`
	case $1 in
		--srate)
			srate=$2
			shift 2
			;;
		--source)
			source=$2
			shift 2
			;;
		--ra)
			ra=$2
			shift 2
			;;
		--dec)
			dec=$2
			shift 2
			;;
		--freq)
			freq=$2
			shift 2
			;;
		--rfgain)
			rfgain=$2
			shift 2
			;;
		--runtime)
			runtime=$2
			shift 2
			;;
		--datadir)
			datadir=$2
			shift 2
			;;
		--extra)
			extra=$2
			shift 2
			;;
		--startt)
			startt=$2
			shift 2
			;;
		*)
			echo "Unknown option $1"
			exit
	esac
done

#
# Determine hardware type
#
lsusb >tmp$$

#
# USRP B2xx
#
if grep -q 2500:002 tmp$$
then
	echo "Detected a USRP B2xx type device"
	devstr="uhd=0,type=b200,num_recv_frames=512"
	if [ @@ != @$extra@ ]
	then
		devstr=${devstr},${extra}
	fi
	promptfor "$srate" 10.0e6 "Sample rate" srate
	srate=$rval
fi

#
# RTL-SDR
#
if grep -q 0bda:28 tmp$$
then
	echo "Detected a RTL-SDR device"
	srate="2.5e6"
	devstr="rtl=0"
fi


#
# AirSpy R2
#
if grep -q 1d50:60a1 tmp$$
then
    echo "Detected an AirSpy R2 device"
	devstr="airspy=0,pack=1"
	valid=0
	while [ $valid == 0 ]
	do
		promptfor "$srate" 6.0e6 "Sample rate" "srate"
		case $rval in
			6.0e6|6e6|6000000)
				valid=1
				;;
			2.5e6|2500000)
				valid=1
				;;
			*)
				echo "Invalid sample rate for AirSpy R2"
		esac
	done
fi

#
# Lime
#
if grep -q 04b4:6108 tmp$$
then
    echo "Detected a LimeSDR device"
	devstr="soapy,driver=lime,nchan=1"
	promptfor "$srate" 8.0e6 "Sample Rate" srate
	srate=$rval
fi

if grep -q 0403:601f tmp$$
then
    echo "Detected a LimeSDR device"
	devstr="soapy,driver=lime,nchan=1"
	promptfor "$srate" 8.0e6 "Sample Rate" srate
	srate=$rval
fi

if grep -q 1d50:6108 tmp$$
then
    echo "Detected a LimeSDR device"
	devstr="soapy,driver=lime,nchan=1"
	promptfor "$srate" 8.0e6 "Sample Rate" srate
	srate=$rval
fi

rm -f tmp$$

if [ $devstr == "unknown" ]
then
	echo "You don't appear to have a supported device on this system"
	echo "Must be: LimeSDR,LimeSDR-Mini,AirSpyR2,USRP-B200"
	exit
fi

promptfor "$source" B0329+54 "Source Name" source
source=$rval

promptfor "$ra" 03.49 "Source RA" ra
ra=$rval

promptfor "$dec" 54.00 "Source DEC" dec
dec=$rval

promptfor "$freq" 612.0e6 "Center Frequency(Hz)" freq
freq=$rval

promptfor "$rfgain" 30.0 "RF Gain" rfgain
rfgain=$rval

promptfor "$runtime" 1 "Runtime (minutes)" runtime
runtime=$rval

promptfor "$datadir" /mnt/data "Data Directory" datadir
datadir=$rval

#
# Spot the race condition...
#
udate=`date -u +%Y%m%d`

#
# Start the GNU-radio based filterbank receiver
#
echo "Starting pulsar filterbank receiver at" `date`
stime=`date +%s`
rlogfile=${source}-${udate}-receiver.log
python2 $HOME/pulsar_filterbank.py --device $devstr --srate $srate --freq $freq --prefix "${datadir}/${source}-" \
        --rfgain $rfgain\
		--runtime `expr $runtime '*' 60`  --startt ${startt} >$rlogfile  2>&1
rval=$?

etime=`date +%s`
echo "Receiver finished at" `date` "with status $rval"

#
# OK, check some health indicators for the receiver run
#

#
# Return code
#
if [ $rval -ne 0 ]
then
	echo "Receiver indicated non-zero return status"
	echo "Showing receiver log"
	cat $rlogfile
	exit
fi

#
# Make sure the elapsed time is correct
#
tdiff=`expr $etime - $stime`
rt=`expr $runtime \* 60`
if [ $tdiff  -lt $rt ]
then
	echo "Receiver did not run long enough to satisfy runtime of $runtime minutes"
	echo "Showing receiver log"
	cat $rlogfile
	exit
fi
